package com.xps.purifierapp.view.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import com.xps.purifierapp.R;
import com.xps.purifierapp.utils.AppCommond;
import com.xps.purifierapp.utils.SharedPreferencesUtils;
import com.xps.purifierapp.view.fragment.HomeFragment;
import com.xps.purifierapp.view.fragment.IntroductionFragment;
import com.xps.purifierapp.view.fragment.StatisticsFragment;
import com.xps.purifierapp.view.wigdet.BottomNavigationViewHelper;
import com.zhy.autolayout.AutoFrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

public class MainActivity extends FragmentActivity {


    @Bind(R.id.navigation)
    BottomNavigationView navigation;
    @Bind(R.id.Fragmen_Home)
    AutoFrameLayout FragmenHome;
    HomeFragment homeFragment;
    IntroductionFragment introductionFragmen;
    StatisticsFragment statisticsFragment;
    private long exitTime = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        BottomNavigationViewHelper.disableShiftMode(navigation);
        homeFragment = new HomeFragment();
        introductionFragmen = new IntroductionFragment();
        statisticsFragment = new StatisticsFragment();
        this.getSupportFragmentManager().beginTransaction().add(R.id.Fragmen_Home, homeFragment).disallowAddToBackStack().commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.Fragmen_Home, introductionFragmen).disallowAddToBackStack().show(introductionFragmen).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.Fragmen_Home, statisticsFragment).disallowAddToBackStack().show(statisticsFragment).commit();
        introductionFragmen.hide();
        statisticsFragment.hide();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AppCommond a) {
        if (!AppCommond.check(a)) return;
        for (String activityName : a.getAcNameList()) {
            if (activityName.equals(getClass().getSimpleName())) {
                homeFragment.NotInstalled();
                homeFragment.Completed();
                statisticsFragment.shuaxin();

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unreg();
    }

    void unreg() {
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    showFragment(homeFragment);
                    return true;
                case R.id.navigation_statistics:
                    showFragment(statisticsFragment);
                    //startActivity(new Intent(MainActivity.this, StatisticsFragment.class));
                    return true;
                case R.id.navigation_password:
                    showFragment(introductionFragmen);
                    return true;
                case R.id.navigation_introduction:
                    showNormalDialog();
                    return false;
            }
            return false;
        }
    };

    public void showFragment(Fragment showFragemt) {
        hideFrags();
        this.getSupportFragmentManager().beginTransaction().disallowAddToBackStack().show(showFragemt).commit();
    }

    public void hideFrags() {
        homeFragment.hide();
        introductionFragmen.hide();
        statisticsFragment.hide();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    private void showNormalDialog() {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainActivity.this);
        normalDialog.setTitle("温馨提示");
        normalDialog.setMessage("是否退出该账号");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        finish();
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        SharedPreferencesUtils.putBoolean("isSave", "Boolean", false);
                        SharedPreferencesUtils.putString("rel_name", "String", "");
                        SharedPreferencesUtils.putString("user_token", "String", "");
                     //   JPushInterface.setAliasAndTags(MainActivity.this, "", null, null);
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        homeFragment.NotInstalled();
        homeFragment.Completed();
        statisticsFragment.shuaxin();
    }
}
